#include "pack.h"

Pack::Pack()
{
    memset(buf, 0, 4096);
    count = 0;
    pack_size = 6;
}

void Pack::append(const QString &val){
    short size = strlen(val.toLocal8Bit().data());
    memcpy(buf+count, &size, 2);
    count += 2;

    memcpy(buf+count, val.toLocal8Bit().data(), size);
    count += size;

    pack_size = 2+count+4;
}

QStringList Pack::getData(){
    int rd_count = 0;
    short size = 0;
    QStringList list;

    while(1){
        memcpy(&size, buf+rd_count,2);
        rd_count += 2;

        char temp[size+1] = {0};
        memcpy(temp, buf+rd_count, size);
        rd_count += size;

        QString data = QString::fromLocal8Bit(temp);
        qDebug() << "data: " << data;
        //if(data.isEmpty()){break;}
        list.append(data);
        if(buf[rd_count]==0){break;}
    }

    clear();
    return list;
}

void Pack::clear(){
    memset(buf, 0, 4096);
    count = 0;
    pack_size = 6;
}
